<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-menu"></i>{{$route.name}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="handle-box">
            <el-dialog :title="title" :visible.sync="dialogFormVisible">
                <el-form :model="addForm" ref="addForm">
                    <el-form-item label="单位" prop="name" :label-width="formLabelWidth">
                        <el-input v-model="addForm.name" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="链接图片" :label-width="formLabelWidth">
                        <el-upload class="avatar-uploader" :data="uploadImg" ref="upload" :action="getLocation" :show-file-list="false" :on-success="handleAvatarSuccess" v-loading.fullscreen.lock="fullscreenLoading" :before-upload="beforeAvatarUpload">
                            <img v-if="addForm.imgUrl" :src="addForm.imgUrl" class="avatar">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </el-form-item>
                    <el-form-item label="链接地址" prop="imgUrl" :label-width="formLabelWidth">
                        <el-input v-model="addForm.linkurl" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="索引" prop="" :label-width="formLabelWidth">
                        <el-input v-model="addForm.sortIndex" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="用户角色" prop="type" :label-width="formLabelWidth">
                        <el-select v-model="addForm.type" placeholder="请选择用户角色">
                            <el-option v-for="(type,index) in types" :key="index" :label="type" :value="index"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="备注" prop="note" :label-width="formLabelWidth">
                        <el-input v-model="addForm.note" auto-complete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="cancel">取 消</el-button>
                    <el-button type="primary" @click="add">确 定</el-button>
                </div>
            </el-dialog>
            <el-input v-model="select_word" placeholder="输入单位名" class="search"></el-input>
            <el-button type="primary" icon="search" @click="search">搜索</el-button>
            <el-button type="primary" @click="beforeAdd">新增合作单位</el-button>
        </div>
        <div>
            <el-table :data="page.content" border fit style="width: 100%">
                <el-table-column prop="name" label="单位名称" show-overflow-tooltip>
                </el-table-column>
                <el-table-column label="图片">
                    <template slot-scope="scope">
                        <img :src="scope.row.imgUrl" width="100%" height="100%" />
                    </template>
                </el-table-column>
                <el-table-column prop="linkurl" label="链接地址" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="type" label="类型" :formatter="type_format">
                </el-table-column>
                <el-table-column prop="note" label="备注" show-overflow-tooltip>
                </el-table-column>
                <el-table-column fixed="right" label="操作">
                    <template slot-scope="scope">
                        <el-button @click="edit(scope.row)" type="text" size="small">编辑</el-button>
                        <el-button type="text" size="small" @click="delFriend(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="block">
            <el-pagination @current-change="handleCurrentChange" :current-page="page.number+1" layout="total, prev, pager, next, jumper" :total="page.totalElements">
            </el-pagination>
        </div>
    </div>
</template>
<script>
import { addFriend, delFriend, putFriend, getFriend } from "../../server/getDate.js";
import { mapState, mapActions } from 'vuex'
import { getStore } from '../../utils/local.js'
import { confirm, location } from '../../utils/local'
export default {
    data() {
        return {
            value: '',
            loading: false,
            dialogTableVisible: false,
            dialogFormVisible: false,
            dialogFormUpdate: false,
            select_word: '',
            addForm: {
            },
            getLocation: location,
            formLabelWidth: '120px',
            types: ['共建机构', '友情链接', '合作媒体', '特别成员单位'],
            title: '',
            fullscreenLoading: false,
            uploadImg: {},
            tableData: []
        }
    },
    created() {
        this.getFriends()
    },
    computed: mapState({
        page: state => state.Friend.page,
        user: state => state.Login.user
    }),
    methods: {
        ...mapActions(['allFriends']),
        getFriends() {
            getFriend()
                .then(res => {
                    if (res.status === 0) {
                        this.allFriends(res.result)
                    } else {
                        this.$message.error(res.message)
                    }
                }, err => this.$message.error('服务器异常'))
        },
        edit(row) {
            this.dialogFormVisible = true
            this.title = '修改合作单位'
            this.addForm = row
        },
        delFriend(row) {
            this.$confirm('此操作将永久删除该合作单位 , 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let userId = this.user.id
                let id = row.id
                delFriend({ id, userId })
                    .then(res => {
                        if (res.status === 0) {
                            this.getFriends()
                            this.$message.info('删除成功')
                        } else {
                            this.$message.error(res.message)
                        }
                    }, err => this.$message.error('服务器异常'))
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        type_format(row) {
            switch (row.type) {
                case 0: return '共建机构'
                case 1: return '友情链接'
                case 2: return '合作媒体'
                case 3: return '特别成员单位'
            }
        },
        handleAvatarSuccess(res, file) {
            this.addForm.imgUrl = res.result
            this.fullscreenLoading = false
        },
        beforeAvatarUpload(file) {
            return this.upload(file)
        },
        upload(file) {
            this.fullscreenLoading = true
            this.uploadImg.type = 0
            this.uploadImg.file = file
        },
        add() {
            console.log('addFrom', this.addForm)
            let userId = this.user.id
            let body = this.addForm
            if (body.id) {
                putFriend({ body, userId })
                    .then(res => {
                        if (res.status === 0) {
                            this.dialogFormVisible = false
                            this.getFriends()
                        } else {
                            this.$message.error(res.message)
                        }
                    }, err => this.$message.error("服务器异常"))
            } else {
                addFriend({ body, userId })
                    .then(res => {
                        if (res.status === 0) {
                            this.dialogFormVisible = false
                            this.getFriends()
                        } else {
                            this.$message.error(res.message)
                        }
                    }, err => this.$message.error('服务器异常'))
            }
        },
        search() {

        },
        beforeAdd() {
            this.dialogFormVisible = true
            this.addForm = {}
            this.title = "新增合作单位"
        },
        handleCurrentChange(val) {
            let field = 'name'
            let keyword = this.select_word
            let index = val
            let size = this.page.size
            getFriend({ field, keyword, index, size })
                .then(res => {
                    if (res.status === 0) {
                        this.getFriends()
                    } else {
                        this.$message.error(res.message)
                    }
                }, err => this.$message.error('服务器异常'))
        },
        cancel() {
            this.dialogFormVisible = false
            this.getFriends()
        }
    }
}
</script>
<style lang="scss" scoped>
.handle-box {
  margin-bottom: 1%;
  .search {
    width: 30%;
  }
}

.block {
  text-align: center;
  margin-top: 1%;
}
</style>
